<template>
    <div class="check-task">
        <div>
            <eagle-image-uploader :images.sync="data.Attachs" class="m-10"></eagle-image-uploader>
            <div class="up-part">
                <div>
                    <eagle-input   label="来源"    readonly  :value="data.OriginType == 1   ? '检查表检查' : data.OriginType == 2  ? '依据检查'    : '随手拍'" ></eagle-input>
                    <eagle-input    label="检查类别"     :value="data.LGDTypeName"  readonly   ></eagle-input>
                    <eagle-input       label="检查项"  rows="3" type="textarea"  :value="data.DetailItemName"    readonly    ></eagle-input>
                <!-- <div>
                    <eagle-row gutter="20" class="dep_sort" >
                        <eagle-col class="sourcesize">{{    data.OriginType == 1   ? "检查表检查" : data.OriginType == 2  ? "依据检查"    : "随手拍" }}
                            <span class="color-blue">{{ data.TName ? "（" + data.TName + "）" : "" }}</span>
                        </eagle-col>
                    </eagle-row>
                </div> -->

                    <div>
                        <eagle-input label="隐患区域" readonly inputalign="right">
                            <template #input>
                                <div style="display: flex; color: #1296db" v-if="!data.IsAssignedArea" @click="showDangerAreaDialog = true">
                                    <img src="@/assets/Images/AppIcon/icon_danger_select.png" alt="" style="height: 20px; width: 20px" />
                                    选择隐患区域
                                </div>
                            </template>
                        </eagle-input>
                    </div>

                    <!--  控制input不可输入 :readonly="data.IsAssignedArea || !data.DangerCustomArea" -->
                    <eagle-input v-model="data.HiddenDangerArea"  isInputText></eagle-input>
                </div>
                <eagle-input title="隐患描述" v-model="data.HiddenDangerDesc" rows="3" autosize type="textarea" placeholder="请输入隐患描述" multiRow isInputText isMust></eagle-input>
                <div>
                    <div>
                        <van-field class="input_text" label="整改建议" input-align="right">
                            <template #input>
                                <div style="display: flex; color: #1296db" @click="_goClassifys()">
                                    <img src="@/assets/Images/AppIcon/icon_danger_select.png" alt="" style="height: 20px; width: 20px" />
                                    从依据库选择
                                </div>
                            </template>
                        </van-field>
                    </div>
                    <van-field v-model="data.CorrectiveAdvise" rows="3" autosize type="textarea" />
                    <div v-if="data.GistSource" class="warning gist-source">
                        依据来源：{{ data.GistSource }}
                    </div>
                </div>
            </div>
            <template v-if="isManager">
                <van-field class="input_text" name="radio" label="隐患等级" required v-if="data.DangerUseLevel">
                    <template #input>
                        <van-radio-group v-model="data.DangerLevel" direction="horizontal">
                            <van-radio :name="item.ID" style="margin: 0 10px 10px 0" v-for="(item, index) in data.DangerLevels" :key="index">{{ item.Name }}</van-radio>
                        </van-radio-group>
                    </template>
                </van-field>

                <div class="down-part">
                    <van-field class="input_text" v-model="data.LGDFullName" label="隐患分类" placeholder="请选择" required readonly @click="showDangerTypeDialog = true">
                        <template #right-icon>
                            <van-icon name="arrow" />
                        </template>
                    </van-field>
                    <van-field class="input_text" name="radio" label="隐患性质" required>
                        <template #input>
                            <van-radio-group v-model="data.HiddenDangerType" direction="horizontal">
                                <van-radio name="1" style="margin: 0 10px 10px 0">一般隐患</van-radio>

                                <van-radio name="2" style="margin: 0 10px 10px 0">重大隐患</van-radio>
                            </van-radio-group>
                        </template>
                    </van-field>
                    
                    <date-picker label="整改期限" labelWidth:96 :required="data.IsMustSubCorrectiveDate" showFastSelect="true" :date.sync="data.CorrectiveDate"></date-picker>
                </div>
            </template>
            <van-field v-if="this.data.Status == 15" class="input_text" label="退回原因" placeholder="暂无" readonly v-model="data.Remarks">
            </van-field>
            <div class="det_tab_item" style="margin-top: 10px" v-if="this.data.Status == 20">
                <div class="operate_det">
                    <div style="margin-bottom: 10px">操作历史记录</div>
                    <div class="operate_list">
                        <van-row class="operate_row_first">
                            <van-col span="4">序号</van-col>
                            <van-col offset="1" span="10">操作</van-col>
                        </van-row>
                        <van-row v-for="(item, index) in this.data.LogList" :key="index" class="operate_row">
                            <van-col span="4">{{ index + 1 }}</van-col>
                            <van-col offset="1" span="18">{{ item.OperateLog }}
                                <div v-if="item.Attachs">
                                    <image-preview :images="item.Attachs"></image-preview>
                                </div>
                            </van-col>
                        </van-row>
                    </div>
                </div>
            </div>
            <template v-if="
          roles.indexOf('admin') != -1 &&
          this.data.OriginType == 3 &&
          !this.data.ID
        ">
            </template>

            <template>
                <div class="bottom-btns">
                    <van-button type="primary" class="submit-btn" @click="save(true, 'pust')">
                        保存
                    </van-button>
                </div>
            </template>
           
            
        </div>
        <!-- 隐患区域 -->
        <danger-area-dialog :multi="false" :show.sync="showDangerAreaDialog" @callback="onDangerAreaCallback" :selectedValue="data.HiddenDangerAreaCode"></danger-area-dialog>
        <!-- 隐患分类 -->
        <danger-type-dialog v-model="showDangerTypeDialog" @callback="onDangerTypeCallback">
        </danger-type-dialog>
        <!-- 部门，人 -->

        <DepartmentDialog
            :show.sync="DepartmentTypeShow"
            @callback="_chooseDepartment"
			:selectedValue="this.data.RectDeptCode"
            >
        </DepartmentDialog>

        <!-- 检查依据 -->
        <!-- <classifys-dialog
      v-model="ClassifysShow"
      @callback="_chooseClassifys"
    >
    </classifys-dialog> -->
        <van-popup v-model="ClassifysShow" closeable position="bottom" :style="{ height: '80%' }">
            <div class="popup-title">选择依据</div>
            <div class="popup-content">
                <Classifys @ClassifysCb="_chooseClassifys" :isShow="isShow" />
            </div>
        </van-popup>

        <BackHiddenDialog :show="showBackDialog" :id="this.data.ID" @cb="onBack" />
    </div>
</template>


<script>
import { mapGetters } from "vuex";
import Classifys from "@/components/Classifys/Classifys.vue";
import store from "@/store/index";
import { Dialog } from "vant";
import BackHiddenDialog from "@/views/Otherdanger/OtherdangerCheckTaskDetail/components/BackHiddenDanger";
import DepartmentAndUserDialog from "@/views/components/department/departmentUserChooseDialog.vue";
import UserChooseDialog from "@/views/components/user/userChooseDialog.vue";
import DangerAreaDialog from "@/views/components/danger/dangerAreaDialog.vue";
import classifysDialog from "@/views/components/danger/classifysDialog.vue";
import DangerTypeDialog from "@/views/components/danger/dangerTypeDialog.vue";
import DepartmentDialog from "./departmentChooseDialog.vue";
export default {
    components: {
        Classifys,
        BackHiddenDialog,
        DangerTypeDialog,
        DangerAreaDialog,
        DepartmentAndUserDialog,
        UserChooseDialog,
        classifysDialog,
        DepartmentDialog
    },
    data() {
        return {
            baseUrl: process.env.VUE_APP_BASE_API,
            data: {
                OriginType: this.$route.query.originType || 3,
                LogList: [],
                DangerSimpleType: false,
            },
            id: "",
            ClassifysShow: false,
            isShow: false,
            showBackDialog: false,
            showDangerAreaDialog: false,
            showDangerTypeDialog: false,
            showDepartmentAndUserDialog: false,
            showUserChooseDialog: false,
            DepartmentTypeShow : false,
        };
    },
    computed: {
        ...mapGetters(["CompanyCode", "roles"]),
        correctiveFullName: {
            get: function () {
                if (this.data.CorrectiveDeptCode) {
                    if (this.data.CorrectiveUserName) {
                        return (
                            this.data.CorrectiveDeptName + "-" + this.data.CorrectiveChnName
                        );
                    } else {
                        return this.data.CorrectiveDeptName;
                    }
                } else {
                    return this.data.CorrectiveChnName;
                }
            },
        },
        isManager: {
            get() {
                return (
                    this.roles.indexOf("admin") != -1 || this.data.DangerSimpleType == "A"
                );
            },
        },
        isSimple: {
            get() {
                return this.data.DangerSimpleType == "A";
            },
        },
        labelCorrectiveDeptUserName: {
            get() {
                return this.isSimple == true ? "整改人" : "整改部门/人";
            },
        },
    },
    created() {
        if (this.$route.query.ctCode) {
            this.ctCode = this.$route.query.ctCode || "";
        }
        this.data.ID = this.$route.query.id || 0;
        this.data.OriginType = this.$route.query.originType || 3;
        if (!this.data.ID) {
            this.initNewEntity();
        } else {
            this.getDangerDetail();
        }
    },
    methods: {
        exit_dialog() {
            //关闭弹窗
            this.isShow = false;
        },
        showDeptUser() {
            if (this.isSimple) {
                this.showUserChooseDialog = true;
            } else {
                this.showDepartmentAndUserDialog = true;
            }
        },
        calcTitle() {
            let title2 = this.$route.query.title;
            if (title2) {
                title = title2;
            }
            let originType = this.data.OriginType;
            let id = this.data.ID;
            if (originType == 1) {
                if (id > 0) {
                    title = "隐患编辑";
                }
            }
            store.commit("updateTitle", title);
        },
        // 计划详情
        initNewEntity() {
            var me = this;
            const param = {
                id: 0,
                ctCode: this.ctCode || "",
                OriginType: this.data.OriginType,
            };
            let _this = this;
            _this.$axios
                .get("/OtherDangerCheckTaskDetailJG/InitNewEntity", { params: param })
                .then((res) => {
                    _this.data = res.data;
                    _this.IsManager = _this.IsManager || _this.data.DangerSimpleType;
                    if (_this.$route.params.file) {
                        _this.data.Attachs = _this.$route.params.file;
                    }
                    if (_this.$route.params.vobj) {

                        _this.data.CorrectiveAdvise =
                            _this.$route.params.vobj.CorrectiveAdvise;
                        _this.data.HiddenDangerDesc =
                            _this.$route.params.vobj.HiddenDangerDesc;
                        _this.data.OriginType = 2;
                        _this.data.LGDName = _this.$route.params.vobj.DName;
                        _this.data.LGDCode = _this.$route.params.vobj.DCode;
                        _this.data.OriginalText = _this.$route.params.vobj.OriginalText;
                        _this.data.GistSource = _this.$route.params.vobj.GistSource;
                        _this.data.LGCode = _this.$route.params.vobj.LGCode;
                        _this.data.LegalLiability = _this.$route.params.vobj.LegalLiability;
                        _this.data.HiddenDangerAreaType =
                            _this.$route.params.vobj.HiddenDangerType;
                        _this.data.LGDType = _this.$route.params.vobj.DType;
                        _this.data.LGDTypeName = _this.$route.params.vobj.DTypeName;
                        _this.data.LGDFullName = _this.$route.params.vobj.DFullName;
                    }
                    // 设备发起检查
                    if (_this.$route.query.areaName) {
                        _this.data.HiddenDangerArea = _this.$route.query.areaName;
                        _this.data.HiddenDangerAreaType = _this.$route.query.areaType;
                        _this.data.HiddenDangerAreaCode = _this.$route.query.areaCode;
                        _this.data.HiddenDangerAreaNo = _this.$route.query.areaNo;
                        _this.data.IsAssignedArea = true;
                    }
                    _this.calcTitle();
                });
        },
        getDangerDetail() {
            const param = {
                id: this.data.ID,
            };
            let _this = this;
            _this.$axios
                .get("/OtherDangerCheckTaskDetailJG/GetDetail", { params: param })
                .then((res) => {
                    _this.data = res.data;

                    _this.calcTitle();
                });
        },
        onDateCallback(date) {
            this.data.CorrectiveDate = date;
        },
        // 点击保存
        save(Confirm, info) {
            // return this.$toast("请上传图片!"+info);
            if (!this.data.Attachs && this.data.OriginType == 3) {
                return this.$toast("随手拍必须选择隐患图片!");
            }
            if (info == "pust") {
                if (!this.data.HiddenDangerDesc) {
                    return this.$toast("提交检查记录必须输入隐患描述");
                }
                if (this.roles.indexOf("admin") != -1) {
                    if (!this.data.LGDName) {
                        return this.$toast("提交检查记录必须选择隐患分类");
                    }
                    if (this.data.IsMustSubCorrectiveDate) {
                        if (!this.data.CorrectiveDate) {
                            return this.$toast("请选择整改期限");
                        }
                    }
                }
            }
            this.loading = true;
            let form = {
                ID: this.data.ID,
                OriginType: this.data.OriginType || 3,
                CompanyCode: this.CompanyCode,
                IsAssignedArea: this.data.IsAssignedArea,
                HiddenDangerDesc: this.data.HiddenDangerDesc,
                CorrectiveAdvise: this.data.CorrectiveAdvise,
                HiddenDangerArea: this.data.HiddenDangerArea,
                HiddenDangerAreaType: this.data.HiddenDangerAreaType,
                HiddenDangerAreaCode: this.data.HiddenDangerAreaCode,
                Attachs: this.data.Attachs,
                HiddenDangerType: this.data.HiddenDangerType,
                DangerLevel: this.data.DangerLevel,
                LegalLiability: this.data.LegalLiability,
                OriginalText: this.data.OriginalText,
                LGCode: this.data.LGCode,
                LGDCode: this.data.LGDCode,
                LGDName: this.data.LGDName,
                LGDType: this.data.LGDType,
                LGDTypeName: this.data.LGDTypeName,
                CorrectiveUserName: this.data.CorrectiveUserName,
                CorrectiveChnName: this.data.CorrectiveChnName,
                CorrectiveDeptCode: this.data.CorrectiveDeptCode,
                CorrectiveDeptName: this.data.CorrectiveDeptName,
                GistSource: this.data.GistSource,
                CorrectiveDate: this.data.CorrectiveDate,
                Submit: Confirm,
                RectDeptCode : this.data.RectDeptCode,
                RectDeptName : this.data.RectDeptName,
            };
            // 检查任务详情
            if (this.ctCode) {
                form.CTCode = this.ctCode;
                form.CompanyCode = this.data.CompanyCode;
            }
            let _this = this;
            if (!_this.data.ID) {
                let originType = this.data.OriginType;
                _this.$axios
                    .post("/ChsDangerNotPorduce/EdtiTaskDetailPost?Submit=" + Confirm, form)
                    .then((res) => {
                        _this.loading = false;
                        if (info == "info") {
                            _this.data.ID = 0;
                            _this.data.Attachs = null;
                            _this.data.OriginType = originType;
                            _this.data.CorrectiveAdvise = null;
                            _this.data.HiddenDangerDesc = null;
                            _this.data.LGDCode = null;
                            _this.data.LGDName = null;
                            _this.data.LGDType = null;
                            _this.data.LGDTypeName = null;
                            _this.data.LGDFullName = null;
                            _this.data.LGCode = null;
                            _this.data.LegalLiability = null;
                            _this.data.OriginalText = null;
                            _this.data.GistSource = null;
                            _this.data.CorrectiveDeptName = null;
                            _this.data.CorrectiveDeptCode = null;
                            _this.data.CorrectiveChnName = null;
                            _this.data.CorrectiveUserName = null;
                            _this.data.HiddenDangerType = "1";
                            return;
                        }
                        setTimeout(() => {
                            _this.$router.go(-1);
                        }, 500);
                    })
                    .catch((err) => {
                        console.log("err", err);
                    });
            } else {
                if (_this.data.Status == 20) {
                    _this.$axios
                        .post("/ChsDangerNotPorduce/EdtiTaskDetailPost?Confirm=" + Confirm, form)
                        .then((res) => {
                            _this.loading = false;
                            setTimeout(() => {
                                _this.$router.goRefresh(-1);
                            }, 500);
                        })
                        .catch((err) => {
                            console.log("err", err);
                        });
                } else {
                    _this.$axios
                        .post("/ChsDangerNotPorduce/EdtiTaskDetailPost?Submit=" + Confirm, form)
                        .then((res) => {
                            _this.loading = false;
                            setTimeout(() => {
                                _this.$router.goRefresh(-1);
                            }, 500);
                        })
                        .catch((err) => {
                            console.log("err", err);
                        });
                }
            }
        },
        // 隐患分类回调
        onDangerAreaCallback(item) {
            if (item) {
                this.$set(this.data, "HiddenDangerArea", item.AreaName);
                this.data.HiddenDangerAreaCode = item.AreaCode;
                this.data.HiddenDangerAreaName = item.AreaName;
                this.data.HiddenDangerAreaType = item.AreaType;

                this.data.CorrectiveDeptCode = item.CorrectiveDeptCode;
                this.data.CorrectiveDeptName = item.CorrectiveDeptName;
                this.data.CorrectiveUserName = item.CorrectiveUserName;
                this.data.CorrectiveChnName = item.CorrectiveChnName;
                this.showDangerAreaDialog = false;
            }
        },
        // 隐患分类回调
        onDangerTypeCallback(item) {
            if (item) {

                this.data.LGDCode = item.DCode;
                this.data.LGDName = item.DName;
                this.data.LGDType = item.DType;
                this.data.LGDTypeName = item.DTypeName;
                this.data.LGDFullName = item.DFullName;
                this.showDangerTypeDialog = false;
            }
        },
        // 选择部门人回调
       		// 选择部门
		_goCheckTaskPeople(backpath) {
			
			this.DepartmentTypeShow = true
		},
		// 选择部门回调
		_chooseDepartment(item) {
			if (item) {
				this.$set(this.data, 'RectDeptName', item.name)
				this.data.RectDeptCode = item.code

				this.DepartmentTypeShow = false
			}
		},
        // 检查依据
        _goClassifys() {
            this.ClassifysShow = true;
            this.isShow = false;
        },

        // 选择检查依据
        _chooseClassifys(classifysData, isShow) {

            // 依据库
            if (classifysData) {
                this.$set(
                    this.data,
                    "CorrectiveAdvise",
                    classifysData.CorrectiveAdvise
                );
                this.data.HiddenDangerType = classifysData.HiddenDangerType;
                this.data.LGDName = classifysData.DName;
                this.data.LGDCode = classifysData.DCode;
                this.data.LGDType = classifysData.DType;
                this.data.LGDTypeName = classifysData.DTypeName;
                this.data.LGDFullName = classifysData.DFullName;
                this.data.LGCode = classifysData.LGCode;
                this.data.GistSource = classifysData.GistSource;
                this.data.OriginalText = classifysData.OriginalText;
                this.data.LegalLiability = classifysData.LegalLiability;
                if (!this.data.HiddenDangerDesc) {
                    this.$set(
                        this.data,
                        "HiddenDangerDesc",
                        classifysData.HiddenDangerDesc
                    );
                } else {
                    Dialog.confirm({
                        confirmButtonText: "覆盖",
                        cancelButtonText: "不覆盖",
                        message: "当前已经输入隐患描述，是否覆盖?",
                    })
                        .then(() => {
                            // on confirm
                            this.$set(
                                this.data,
                                "HiddenDangerDesc",
                                classifysData.HiddenDangerDesc
                            );
                            // this.data.HiddenDangerDesc = this.$route.params.classifysData.HiddenDangerDesc
                        })
                        .catch(() => {
                            // on cancel
                        });
                }
                this.ClassifysShow = false;
            }
            if (isShow) {
                this.isShow = true;
            } else {
                this.isShow = false;
            }
        },
        backHiddenDanger() {
            this.showBackDialog = true;
        },
        onBack(result) {
            if (result) {
                this.$toast("退回成功");
                _this.$router.goRefresh(-1);
            }
            this.showBackDialog = false;
        },
    },
};
</script>

<style lang="less" scoped>
.check-task {
    background-color: #e8f1f8;
    padding-bottom: 20px;
}
.problem {
    margin-bottom: 10px;
}
.bottom-btns {
    margin-top: 10px;
    padding: 0 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .submit-btn {
        width: 100%;
        height: 35px;
        font-size: 14px;
    }
    .delete-btn {
        width: 100%;
        height: 35px;
        font-size: 14px;
    }
}
.warning {
    background-color: #fff;
    padding: 10px;
    font-size: 10px;
}

.det_tab_item {
    padding: 10px 0;
    background-color: #fff;
    border-radius: 5px;
    margin-bottom: 10px;

    .det_tab_item_text {
        padding: 0 10px;
    }
}
.operate_det {
    background-color: #fff;
    padding: 10px;
    .operate_list {
        font-size: 12px;
        border: 1px solid #e8f1f8;
        .operate_row_first {
            padding: 10px;
            background-color: #e8f1f8;
            border-bottom: 1px solid #e8f1f8;
            margin-bottom: 10px;
        }
        .operate_row {
            padding: 10px;
            border-bottom: 1px solid #e8f1f8;
            line-height: 16px;
        }
    }
}
.week {
    color: rgb(0, 195, 224);
}
.dep_sort {
  // display: flex;
  // flex-wrap: wrap;
  // margin-bottom: 5px;
  font-size: 18px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.sourcesize{
    font-size: 13px;
    margin-left: 135px;
}
.color-blue{
    font-size: 14px;
}
</style>
